package cn.bxshey.auth.controller;

import cn.bxshey.auth.config.BeanFactoryCustomize;
import cn.bxshey.auth.domain.dto.LoginDto;
import cn.bxshey.auth.domain.vo.UserDetails;
import cn.bxshey.auth.service.LoginService;
import cn.bxshey.general.Result.Result;
import cn.bxshey.general.aop.note.SystemLog;
import cn.bxshey.general.util.UploadTheFile;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

/**
 * 认证中心服务
 */
@RestController
@RequestMapping("/attestation")
public class LoginController {


    /**
     * 认证中心服务测试
     * @return String
     */
    @GetMapping("/test")
    @SystemLog(value = "认证中心服务测试", serve = "1001")
    public String test(String s) {
        System.err.println(s);
        return "认证服务测试成功";
    }



    /**
     * 登录认证
     * 描述：微信、手机认证的统一登录接口
     *
     * @param: 认证类型、openID...
     * @return: 令牌、用户信息
     */
    @PostMapping("/login")
    @SystemLog(value = "登录认证", serve = "1001")
    public Result<Map<String, Object>> loginAuth(@RequestParam(name = "authType") String authType, @RequestBody LoginDto loginDto) throws JsonProcessingException {
        Object logic;
        try { //根据bean类型获取该bean的示例，不存在则抛出异常,停止该请求
            logic = BeanFactoryCustomize.getBean(authType);
            //判断是否是自定义登录类型的bean
            if (!(logic instanceof LoginService)) {
                return Result.error();
            }
        } catch (Exception e) {
            return Result.error();
        }

        // 生成登录业务对象
        LoginService loginService = (LoginService) logic;

        //封装授权码为一个map对象
        Map<String, String> info = new HashMap<>();
        info.put("js_code", loginDto.getOpenid());

        // 调用接口，返回信息
        return Result.success(loginService.login(info));
    }


    @GetMapping("/check")
    public void loginOut() {

    }
}
